import moment from 'moment'

function getMonth(yearArray,TMonth,EMonth){
    let slefYear = yearArray.slice()
    let startYear = slefYear.shift()
    let endYear = slefYear.pop()
    let startMonth=[],endMonth=[],centerMonth=[];
    
    for(let i=TMonth;i<12+1;i++){
        startMonth.push(startYear + '/' + i.toString() + '/1')
    }
    for(let i=1;i<EMonth+1;i++){
        endMonth.push(endYear + '/' + i.toString() + '/1')
    }
    if(slefYear.length!=0){
        for(let i=0;i<slefYear.length;i++){
            for(let k=1;k<12+1;k++){
                centerMonth.push(slefYear[i] + '/' + k.toString() + '/1')
            }
        }
    }
    // console.log([...startMonth,...centerMonth,...endMonth])
    return [...startMonth,...centerMonth,...endMonth]
}
function monthGetDay(begin,end){
    let days = []
    let bTime = moment(begin).date()
    let eTime = moment(end).date()
    let y = moment(end).format('Y')
    let m = moment(end).format('M')
    for(var i=bTime;i<eTime+1;i++){
        days.push({
            date: (m + '/'+ i).toString(),
            day: i.toString(),
            month: m.toString(),
            year: y.toString(),
            disable: false,
            morning: false,
            afternoon: false,
            evening: false
        })
    }
    return days
}
function getDay(monthArray,bTime,eTime){
    let slefMonth = monthArray.slice()
    let startMonth = slefMonth.shift()
    let endMonth = slefMonth.pop()
    let startDaysInMonth = moment(startMonth).daysInMonth()
    let startDays=[],endDays=[],centerDays=[];
    let firstM = moment(startMonth).format("M"),lastM = moment(endMonth).format("M"),firstY = moment(startMonth).format("Y"),lastY = moment(endMonth).format("Y");
    // console.log(firstM,lastM)
    for(let i=bTime;i<startDaysInMonth+1;i++){
        let day = firstM + '/' + i.toString()
        
        startDays.push({
            date: day.toString(),
            day: i.toString(),
            month: firstM.toString(),
            year: firstY.toString(),
            disable: false,
            morning: false,
            afternoon: false,
            evening: false
        })
    }
    for(let i=1;i<eTime+1;i++){
        let day = lastM + '/' + i.toString()
        endDays.push({
            date: day.toString(),
            day: i.toString(),
            month: lastM.toString(),
            year: lastY.toString(),
            disable: false,
            morning: false,
            afternoon: false,
            evening: false
        })
    }
    if(slefMonth.length!=0){
        for(let i=0;i<slefMonth.length;i++){
            let daysInMonth = moment(slefMonth[i]).daysInMonth(),nowMonth = moment(slefMonth[i]).format("M"),nowYear = moment(slefMonth[i]).format("Y");
            for(let k=1;k<daysInMonth+1;k++){
                let day = nowMonth + '/' + k.toString()
                
                centerDays.push({
                    date: day.toString(),
                    day: k.toString(),
                    month: nowMonth.toString(),
                    year: nowYear.toString(),
                    disable: false,
                    morning: false,
                    afternoon: false,
                    evening: false
                })
            }
        }
    }
    return [...startDays,...centerDays,...endDays]
}

export function formatPackTime(begin,end) {
    let yearArray=[],monthArray=[];
    let bTime = moment(begin).date()
    let eTime = moment(end).date()
    let TYear = Number(moment(begin).format("Y"))
    let EYear = Number(moment(end).format("Y"))
    let TMonth = Number(moment(begin).format("M"))
    let EMonth = Number(moment(end).format("M"))
    for( let i=TYear;i<EYear+1;i++){
        yearArray.push(i.toString())
    }
    if(yearArray.length>1){
        monthArray = getMonth(yearArray,TMonth,EMonth)
    }else{
        for( let i=TMonth;i<EMonth+1;i++){
            monthArray.push(yearArray[0] + '/' + i.toString() + '/1')
        }
    }
    let centerdays
    if(monthArray.length<2){
     //开始和结束是同一月
        centerdays = monthGetDay(begin,end)
    }else{
        centerdays = getDay(monthArray,bTime,eTime)
    }
    

    //补全开头结尾星期天数
    let bDay = moment(begin).day(), eDay = moment(end).day();

    let completeStart = [], completeEnd = [];
    for(let i=1;i<bDay+1;i++){
        let bDayMinus = moment(begin).subtract(i, 'd')
        let y = moment(bDayMinus).format('Y'), m = moment(bDayMinus).format('M'), d = moment(bDayMinus).format('D');
        completeStart.unshift({
            date: m + '/' + d,
            day: d.toString(),
            month: m.toString(),
            year: y.toString(),
            disable: true,
            morning: false,
            afternoon: false,
            evening: false
        })
    }
    for(let i=1;i<(7-Number(eDay));i++){
        let bDayAdd = moment(end).add(i, 'd')
        let y = moment(bDayAdd).format('Y'), m = moment(bDayAdd).format('M'), d = moment(bDayAdd).format('D');
        completeEnd.push({
            date: m + '/' + d,
            day: d.toString(),
            month: m.toString(),
            year: y.toString(),
            disable: true,
            morning: false,
            afternoon: false,
            evening: false
        })
    }
    return [...completeStart,...centerdays,...completeEnd]
}


export function formatCourseList(course) {
    let classPlanList = []
    if (!course || course.length === 0) return classPlanList
    course.map(item=>{
        item.classPlanList.map(citem=>{
          if (item.historyCourse){
            citem.courseStatus = item.historyCourse.courseStatus
            citem.hcId = item.historyCourse.id
          }else{
            citem.noApply = true
            citem.courseStatus = '0'
            citem.packId = item.packId
          }
          classPlanList.push(citem)
        })
    })
    return classPlanList
}